//
//  SubProjectInfoCell.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 1/9/17.
//  Copyright © 2017 com.mask. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SubProjectInfoCell: UITableViewCell {
    
    let subProjectName = UILabel()
    let defaultImageView = UIImageView()
    let descriptor = UILabel()
    
    var data: AnyObject? {
        willSet {
            if let process = newValue as? SubProjectProcess {
                subProjectName.text = process.names
                if let status = process.status {
                    switch status {
                    case .pass:
                        descriptor.backgroundColor = UIColor.green
                        break
                    default:
                        descriptor.backgroundColor = UIColor.red
                        break
                    }
                }
            } else {
                if let subproject = newValue as? SubProject {
                    subProjectName.text = subproject.names
                    descriptor.text = subproject.progress
                }
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
        // subProjectName
        addSubview(subProjectName)
        subProjectName.isUserInteractionEnabled = true
        subProjectName.snp.makeConstraints({ make in
            make.top.right.equalTo(self)
            make.left.equalTo(self).offset(Padding.left)
            make.height.equalTo(35)
        })
        
        // default imageview
        addSubview(defaultImageView)
        defaultImageView.backgroundColor = UIColor.red
        defaultImageView.contentMode = .scaleAspectFill
        defaultImageView.layer.masksToBounds = true
        defaultImageView.sd_setImage(with: URL(string: "http://i2.hdslb.com/bfs/archive/5dad81a9ec2fb45d345625e48f7cc1548c0b5274.jpg"), placeholderImage: UIImage(named: "placeholderImage.jpg"))
        defaultImageView.snp.makeConstraints({ make in
            make.top.equalTo(subProjectName.snp.bottom)
            make.bottom.equalTo(self).offset(-35)
            make.left.right.equalTo(self)
        })
        
        // descriptor
        descriptor.text = "4/15"
        descriptor.textAlignment = .center
        addSubview(descriptor)
        descriptor.snp.makeConstraints({ make in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(35)
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
